﻿using MediatR;
using U.B.DoMain;
using U.B.ErrorCode;
using U.B.Repistory;
using U.B.WriteApi.Command;

namespace U.B.WriteApi.CommandHander
{
    public class DelRoleMenuCommandHander : IRequestHandler<DelRoleMenuCommand, ApiResult<int>>
    {
        public IRepistory<RoleMenu> RoleMenuRepistory;

        public DelRoleMenuCommandHander(IRepistory<RoleMenu> roleMenuRepistory)
        {
            RoleMenuRepistory = roleMenuRepistory;
        }
        /// <summary>
        /// 角色菜单删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(DelRoleMenuCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var roleMenu = RoleMenuRepistory.GetAll().Where(x => x.RoleId == request.RoleId).ToList();
            if (roleMenu==null)
            {
                result.code = Renum.删除失败;
                result.Message=Renum.删除失败.ToString();
            }
            var delRoleMenu = RoleMenuRepistory.DelRange(roleMenu);
            result.code=Renum.删除成功;
            result.Message=Renum.删除成功.ToString();
            return Task.FromResult(result);
        }
    }
}
